{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e841dff0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-02T14:52:50.528566Z",
     "start_time": "2024-03-02T14:52:50.417756Z"
    }
   },
   "outputs": [],
   "source": [
    "import json\n",
    "import pangu\n",
    "import os\n",
    "import urllib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "46c978d9",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-02T14:52:51.345220Z",
     "start_time": "2024-03-02T14:52:51.279180Z"
    }
   },
   "outputs": [],
   "source": [
    "with open('plant.json', 'r', encoding='utf-8') as fp:\n",
    "    plant = json.load(fp)\n",
    "    \n",
    "for a in plant.values():\n",
    "    for b in a:\n",
    "        if '植' in b['name']:\n",
    "            b['name'] = b['information']['基本信息']['中文名称']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "d02dc783",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-02T14:52:52.705029Z",
     "start_time": "2024-03-02T14:52:52.265412Z"
    }
   },
   "outputs": [],
   "source": [
    "# md = ''\n",
    "# for name, category in plant.items():\n",
    "#     md += f'## {name}\\n'\n",
    "#     for e in category:\n",
    "#         for k, v in e.items():\n",
    "#             if k == 'name':\n",
    "#                 if '亲植' in v or '调研' in v or '植树' in v:\n",
    "#                     v = e['information']['基本信息']['中文名称']\n",
    "#                 md += f'### {v}\\n'\n",
    "#             elif k == 'cover':\n",
    "#                 md += f'![封面]({v})\\n'\n",
    "#             elif k == 'information':\n",
    "#                 for m, n in v.items():\n",
    "#                     if isinstance(n, dict):\n",
    "#                         md += f'#### {m}\\n'\n",
    "#                         head, sep, table = '|', '|', '|'\n",
    "#                         for a, b in n.items():\n",
    "#                             head += f'{a}|'\n",
    "#                             sep += '-|'\n",
    "#                             table += f'{b}|'\n",
    "#                         md += f'{head}\\n'\n",
    "#                         md += f'{sep}\\n'\n",
    "#                         md += f'{table}\\n'\n",
    "#                     else:\n",
    "#                         if m == '名人简介':\n",
    "#                             md += f'#### {m}\\n'\n",
    "#                             for o in n:\n",
    "#                                 md += f'{pangu.spacing_text(o)}\\n'\n",
    "#                         elif len(n) > 0 and len(n[0]) > 0:\n",
    "#                             md += f'#### {m}\\n'\n",
    "#                             for s in n:\n",
    "#                                 md += f'{pangu.spacing_text(s)}\\n'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "dd71b44f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# with open('plant.md', 'w', encoding='utf-8') as fp:\n",
    "#     fp.write(md)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1321e8d2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "d100c2de",
   "metadata": {},
   "outputs": [],
   "source": [
    "lake = '<!doctype lake><title>plants</title><meta name=\"paragraphSpacing\" content=\"relax\"/>'\n",
    "for name, category in plant.items():\n",
    "    lake += f'<h2><span>{name}</span></h2>'\n",
    "    for e in category:\n",
    "        for k, v in e.items():\n",
    "            if k == 'name':\n",
    "                lake += f'<h3 style=\"padding-left: 2em\"><span>{v}</span></h3>'\n",
    "            elif k == 'cover':\n",
    "                v = {\n",
    "                    'src': v,\n",
    "                    'originalType': 'binary',\n",
    "                    'width': 228,\n",
    "                    'height': 180,\n",
    "                    'linkTarget': '',\n",
    "                    'originWidth': 228,\n",
    "                    'originHeight': 180,\n",
    "                    'ratio': 1,\n",
    "                    'status': 'done',\n",
    "                    'style': 'none',\n",
    "                    'showTitle': False,\n",
    "                    'title': '',\n",
    "                    'rotation': 0,\n",
    "                    'crop': [0, 0, 1, 1],\n",
    "                }\n",
    "                v = urllib.parse.quote(json.dumps(v))\n",
    "                lake += f'<p style=\"text-align: center\"><span><br/></span><card type=\"inline\" name=\"image\" value=\"data:{v}\"></card></p>'\n",
    "            elif k == 'information':\n",
    "                for m, n in v.items():\n",
    "                    if isinstance(n, dict):\n",
    "                        lake += f'<h4 style=\"padding-left: 2em\"><span>{m}</span></h4>'\n",
    "                        info = {\n",
    "                            'rows': len(n),\n",
    "                            'cols': 2,\n",
    "                            'html': None\n",
    "                        }\n",
    "                        html = '<table class=\"lake-table\" margin=\"true\" hindent=\"2em\" style=\"\"><colgroup><col /><col /></colgroup><tr><th>属性</th><th>取值</th></tr><tbody>'\n",
    "                        for a, b in n.items():\n",
    "                            html += f'<tr><td>{a}</td><td>{b}</td></tr>'\n",
    "                        html += '</tbody></table>'\n",
    "                        info['html'] = html\n",
    "                        info = urllib.parse.quote(json.dumps(info))\n",
    "                        lake += f'<card type=\"block\" name=\"table\" value=\"data:{info}\"></card>'\n",
    "                    else:\n",
    "                        if m == '名人简介':\n",
    "                            lake += f'<h4 style=\"padding-left: 2em\"><span>{m}</span></h4>'\n",
    "                            for o in n:\n",
    "                                lake += f'<p style=\"padding-left: 2em\"><span>{indent(2)}{pangu.spacing_text(o)}</span></p>'\n",
    "                        elif len(n) > 0 and len(n[0]) > 0:\n",
    "                            lake += f'<h4 style=\"padding-left: 2em\"><span>{m}</span></h4>'             \n",
    "                            for s in n:\n",
    "                                lake += f'<p style=\"padding-left: 2em\"><span>{indent(2)}{pangu.spacing_text(s)}</span></p>'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "917476a6",
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('plants.lake', 'w', encoding='utf-8') as fp:\n",
    "    fp.write(lake)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bce40980",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "48583212",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-02T14:52:56.526929Z",
     "start_time": "2024-03-02T14:52:56.497267Z"
    }
   },
   "outputs": [],
   "source": [
    "with open('species.json', 'r', encoding='utf-8') as fp:\n",
    "    species = json.load(fp)\n",
    "    \n",
    "def indent(num = 4):\n",
    "    return '&emsp;' * num\n",
    "\n",
    "keys = ['物种名片', 'cover', '分类信息', '形态特征', '生态习性', '功用价值', \n",
    "        '基因序列', '名称 & 分类', '植物志', '物种保护', '标本分布']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ca4ec4ed",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-02T14:52:58.327415Z",
     "start_time": "2024-03-02T14:52:58.169964Z"
    }
   },
   "outputs": [],
   "source": [
    "# md = ''\n",
    "# for name, category in plant.items():\n",
    "#     md += f'## {name}\\n'\n",
    "#     for a in category:\n",
    "#         if a['name'] not in species.keys():\n",
    "#             continue\n",
    "#         for b in [species[a['name']]]:\n",
    "#             md += f'### {a[\"name\"]}\\n'\n",
    "#             for k in keys:\n",
    "#                 v = b.get(k, None)\n",
    "#                 if v is not None and len(v) != 0:\n",
    "#                     if k == 'cover':\n",
    "#                         v = urllib.parse.quote(os.path.basename(v))\n",
    "#                         md += f'![图片](https://chisheng-qingcheng.oss-cn-beijing.aliyuncs.com/image/{v})\\n'\n",
    "#                         continue\n",
    "                    \n",
    "#                     md += f'#### {k}\\n'\n",
    "#                     if isinstance(v, str):\n",
    "#                         v = v.replace(\"\\n\", \"\")\n",
    "#                         md += f'{pangu.spacing_text(v)}\\n'\n",
    "#                     elif isinstance(v, dict):\n",
    "#                         for x, y in v.items():\n",
    "#                             if isinstance(y, str):\n",
    "#                                 y = y.replace(\"\\n\", \"\")\n",
    "#                                 md += f'{indent(2)}**{x}**：{pangu.spacing_text(y)}\\n'\n",
    "#                             elif isinstance(y, dict):\n",
    "#                                 md += f'{indent(2)}**{x}**：\\n'\n",
    "#                                 for o, p in y.items():\n",
    "#                                     md += f'{indent(4)}**{o}**：{p}\\n'\n",
    "#                             elif isinstance(y, list) and len(y) > 0:\n",
    "#                                 md += f'{indent(2)}**{x}**：{\"、\".join(y)}\\n'\n",
    "#                     elif isinstance(v, list):\n",
    "#                         for o in v:\n",
    "#                             if o != '':\n",
    "#                                 md += f'{indent(2)}{o}\\n'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "03ebf5cc",
   "metadata": {},
   "outputs": [],
   "source": [
    "# with open('species.md', 'w', encoding='utf-8') as fp:\n",
    "#     fp.write(md)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b6f8d3ad",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "98a61d3b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-02T16:14:26.416461Z",
     "start_time": "2024-03-02T16:14:25.999069Z"
    }
   },
   "outputs": [],
   "source": [
    "lake = '<!doctype lake><title>species</title><meta name=\"paragraphSpacing\" content=\"relax\"/>'\n",
    "for name, category in plant.items():\n",
    "    lake += f'<h2><span>{name}</span></h2>'\n",
    "    for a in category:\n",
    "        if a['name'] not in species.keys():\n",
    "            continue\n",
    "        for b in [species[a['name']]]:\n",
    "            lake += f'<h3 style=\"padding-left: 2em\"><span>{a[\"name\"]}</span></h3>'\n",
    "            for k in keys:\n",
    "                v = b.get(k, None)\n",
    "                if v is not None and len(v) != 0:\n",
    "                    if k == 'cover':\n",
    "                        v = urllib.parse.quote(os.path.basename(v))\n",
    "                        v = {\n",
    "                            'src': f'https://chisheng-qingcheng.oss-cn-beijing.aliyuncs.com/image/{v}',\n",
    "                            'originalType': 'binary',\n",
    "                            'width': 339,\n",
    "                            'height': 339,\n",
    "                            'linkTarget': '',\n",
    "                            'originWidth': 452,\n",
    "                            'originHeight': 452,\n",
    "                            'ratio': 1,\n",
    "                            'status': 'done',\n",
    "                            'style': 'none',\n",
    "                            'showTitle': False,\n",
    "                            'title': '',\n",
    "                            'rotation': 0,\n",
    "                            'crop': [0, 0, 1, 1],\n",
    "                        }\n",
    "                        v = urllib.parse.quote(json.dumps(v))\n",
    "                        lake += f'<p style=\"text-align: center\"><span><br/></span><card type=\"inline\" name=\"image\" value=\"data:{v}\"></card></p>'\n",
    "                        continue\n",
    "                    lake += f'<h4 style=\"padding-left: 2em\"><span>{k}</span></h4>'\n",
    "                    if isinstance(v, str):\n",
    "                        v = v.replace(\"\\n\", \"\")\n",
    "                        v = pangu.spacing_text(v)\n",
    "                        lake += f'<p style=\"padding-left: 4em\"><span>{v}</span></p>'\n",
    "                    elif k == '植物志' and len(v) != 0:\n",
    "                        lake += f'<p style=\"padding-left: 4em\"><strong><span>{v[\"标题\"]}</span></strong></p>'\n",
    "                        for o in v['正文']:\n",
    "                            lake += f'<p style=\"padding-left: 4em\"><span>{indent(2)}{pangu.spacing_text(o)}</span></p>'\n",
    "                    elif isinstance(v, dict):\n",
    "                        lake += f'<p style=\"padding-left: 4em\">'\n",
    "                        for x, y in v.items():\n",
    "                            if isinstance(y, str):\n",
    "                                y = y.replace(\"\\n\", \"\")\n",
    "                                y = pangu.spacing_text(y)\n",
    "                                lake += f'<strong><span>{x}</span></strong><span>：{y}<br /></span>'\n",
    "                            elif isinstance(y, dict):\n",
    "                                lake += f'<strong><span>{x}</span></strong><span>：<br /></span>'\n",
    "                                for o, p in y.items():\n",
    "                                    lake += f'<strong><span>{indent(2)}{o}</span></strong><span>：{p}<br /></span>'\n",
    "                            elif isinstance(y, list) and len(y) > 0:\n",
    "                                y = \"、\".join(y)\n",
    "                                lake += f'<strong><span>{x}</span></strong><span>：{y}<br /></span>'\n",
    "                        lake += '</p>'\n",
    "                    elif isinstance(v, list):\n",
    "                        lake += f'<p style=\"padding-left: 4em\"><span>'\n",
    "                        for o in v:\n",
    "                            if o != '':\n",
    "                                lake += f'{o}<br />'\n",
    "                        lake += '</span></p>'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "7415f15b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-03-02T16:14:27.947228Z",
     "start_time": "2024-03-02T16:14:27.933749Z"
    }
   },
   "outputs": [],
   "source": [
    "with open('species.lake', 'w', encoding='utf-8') as fp:\n",
    "    fp.write(lake)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fceb4d13",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "pytorch-env",
   "language": "python",
   "name": "pytorch-env"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
